package com.ruoyi.controller.system;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.UserIsAdminType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysChannel;
import com.ruoyi.system.domain.SysOrder;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysChannelService;
import com.ruoyi.system.service.ISysOrderService;
import com.ruoyi.system.service.ISysUserBillcashService;
import com.ruoyi.system.vo.OrderStatisticsRes;
import com.ruoyi.system.vo.req.SysOrderReq;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单Controller
 *
 * @author Ericcc
 * @date 2021-03-01
 */
@Controller
@RequestMapping("/system/order")
public class SysOrderController extends BaseController
{
    private String prefix = "system/order";

    @Autowired
    private ISysOrderService sysOrderService;

    @Autowired
    private ISysChannelService sysChannelService;

    @Autowired
    private ISysUserBillcashService sysUserBillcashService;

    @RequiresPermissions("system:order:list")
    @GetMapping()
    public String order(ModelMap mmap)
    {
        SysUser user = ShiroUtils.getSysUser();
        mmap.put("user",user);
        List<SysChannel> sysChannels = sysChannelService.selectSysChannelList(new SysChannel());
        mmap.put("sysChannels",sysChannels);
        return prefix + "/list";
    }

    /**
     * 修改订单
     */
    @GetMapping("/edit/{orderId}")
    public String edit(@PathVariable("orderId") Long orderId, ModelMap mmap)
    {
        SysOrder sysOrder = sysOrderService.selectSysOrderById(orderId);
        mmap.put("sysOrder", sysOrder);
        return prefix + "/edit";
    }

    /**
     * 查询订单列表
     */
    @RequiresPermissions("system:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysOrderReq sysOrder,ModelMap mmap)
    {
        startPage();
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        if (user.getUserType()== UserIsAdminType.COMMON.getCode()){
            sysOrder.setUserCode(user.getUserCode());
        }else if(user.getUserType()== UserIsAdminType.AGENT.getCode()){
            List<SysOrder> list = sysOrderService.selectSysAgentOrder(sysOrder,user.getUserId());
            return getDataTable(list);
        }else if(user.getUserType()== UserIsAdminType.HIGHAGENT.getCode()){
            List<SysOrder> list = sysOrderService.selectSysHighAgentOrder(sysOrder,user.getUserId());
            return getDataTable(list);
        }
        List<SysOrder> list = sysOrderService.selectSysOrderList(sysOrder);
        return getDataTable(list);
    }

    @RequiresPermissions("system:order:list")
    @PostMapping("/showStatistics")
    @ResponseBody
    public OrderStatisticsRes showStatistics(SysOrderReq sysOrder,ModelMap mmap)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        if (user.getUserType()== UserIsAdminType.COMMON.getCode()){
            sysOrder.setUserCode(user.getUserCode());
        }else if(user.getUserType()== UserIsAdminType.AGENT.getCode()){
            Map map = new HashMap();
            map.put("parentId",user.getUserId());
            sysOrder.setParams(map);
            OrderStatisticsRes orderStatisticsRes = sysOrderService.selectAgentSysOrderStatistics(sysOrder);
            Date todayBegin = DateUtil.beginOfDay(new Date());
            Date monthBegin = DateUtil.beginOfMonth(new Date());
            orderStatisticsRes.setTodayOrderAmount(sysOrderService.selectAgentSysOrderAmount(sysOrder,todayBegin));
            orderStatisticsRes.setMonthOrderAmount(sysOrderService.selectAgentSysOrderAmount(sysOrder,monthBegin));
            //查询代理自己的佣金记录
            sysOrder.setUserCode(user.getUserCode());
            orderStatisticsRes.setTodayAgentCommission(sysUserBillcashService.selectSysAgentBillcashStatistics(sysOrder,todayBegin));
            orderStatisticsRes.setMonthAgentCommission(sysUserBillcashService.selectSysAgentBillcashStatistics(sysOrder,monthBegin));
            return orderStatisticsRes;
        }else if(user.getUserType()== UserIsAdminType.HIGHAGENT.getCode()){
            Map map = new HashMap();
            map.put("parentId",user.getUserId());
            sysOrder.setParams(map);
            OrderStatisticsRes orderStatisticsRes = sysOrderService.selectHighAgentSysOrderStatistics(sysOrder);
            Date todayBegin = DateUtil.beginOfDay(new Date());
            Date monthBegin = DateUtil.beginOfMonth(new Date());
            orderStatisticsRes.setTodayOrderAmount(sysOrderService.selectHighAgentSysOrderAmount(sysOrder,todayBegin));
            orderStatisticsRes.setMonthOrderAmount(sysOrderService.selectHighAgentSysOrderAmount(sysOrder,monthBegin));
            //查询代理自己的佣金记录
            sysOrder.setUserCode(user.getUserCode());
            orderStatisticsRes.setTodayAgentCommission(sysUserBillcashService.selectSysAgentBillcashStatistics(sysOrder,todayBegin));
            orderStatisticsRes.setMonthAgentCommission(sysUserBillcashService.selectSysAgentBillcashStatistics(sysOrder,monthBegin));
            return orderStatisticsRes;
        }
        OrderStatisticsRes orderStatisticsRes = sysOrderService.selectSysOrderStatistics(sysOrder);
        Date todayBegin = DateUtil.beginOfDay(new Date());
        Date monthBegin = DateUtil.beginOfMonth(new Date());
        orderStatisticsRes.setTodayOrderAmount(sysOrderService.selectSysOrderAmount(sysOrder,todayBegin));
        orderStatisticsRes.setMonthOrderAmount(sysOrderService.selectSysOrderAmount(sysOrder,monthBegin));
        orderStatisticsRes.setTodayAgentCommission(sysUserBillcashService.selectSysUserBillcashStatistics(sysOrder,todayBegin));
        orderStatisticsRes.setMonthAgentCommission(sysUserBillcashService.selectSysUserBillcashStatistics(sysOrder,monthBegin));
        return orderStatisticsRes;
    }

    /**
     * 导出订单列表
     */
    @RequiresPermissions("system:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysOrderReq sysOrder)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        if (user.getUserType()== UserIsAdminType.COMMON.getCode()){
            sysOrder.setUserCode(user.getUserCode());
        }else if( user.getUserType()== UserIsAdminType.AGENT.getCode()){
            List<SysOrder> list = sysOrderService.selectSysAgentOrder(sysOrder,user.getUserId());
            ExcelUtil<SysOrder> util = new ExcelUtil<SysOrder>(SysOrder.class);
            return util.exportExcel(list, "order");
        }else if(user.getUserType()== UserIsAdminType.HIGHAGENT.getCode()){
            List<SysOrder> list = sysOrderService.selectSysHighAgentOrder(sysOrder,user.getUserId());
            ExcelUtil<SysOrder> util = new ExcelUtil<SysOrder>(SysOrder.class);
            return util.exportExcel(list, "order");
        }
        List<SysOrder> list = sysOrderService.selectSysOrderList(sysOrder);
        ExcelUtil<SysOrder> util = new ExcelUtil<SysOrder>(SysOrder.class);
        return util.exportExcel(list, "order");
    }


    /**
     * 删除订单
     */
    @RequiresPermissions("system:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysOrderService.deleteSysOrderByIds(ids));
    }
}
